﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
//using ExcelDataReader;
using Newtonsoft.Json.Linq;
using Luna小店.Utils;
using Luna小店.DTO;
using Luna小店.service;
using DevExpress.XtraGrid.Demos.util;
using mendian;

namespace Luna小店.原型
{
    public partial class XtraFormEXCEL : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public List<DTO库存一览> list = new List<DTO库存一览>();
        public XtraFormEXCEL()
        {
            InitializeComponent();
            this.list = FunctionHelper.JsonDeserialization<List<DTO库存一览>>(RestCall.httpGetMethod(URL.接口url + "/inventory/stockall"));
            this.gridControl1.DataSource = this.list;
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataSet result = ExcelUtil.getExcelDatas();

            DataTable dataTablePoint = result.Tables["默认设计点"];
            List<DTO默认设计点> listPoint = new List<DTO默认设计点>();
            foreach (DataRow dataRow in dataTablePoint.Rows)
            {
                listPoint.Add(new DTO默认设计点(dataRow));
            }

            DataTable dataTableSize = result.Tables["尺寸"];
            List<DTO尺寸> listSize = new List<DTO尺寸>();
            foreach (DataRow dataRow in dataTableSize.Rows)
            {
                listSize.Add(new DTO尺寸(dataRow));
            }
            this.gridControl1.DataSource = listSize;

            //List<InventoryDto> inventoryDtos = new List<InventoryDto>();
            //foreach (DataTable dataTable in result.Tables)
            //{
            //    foreach (DataRow dataRow in dataTable.Rows)
            //    {
            //        if (dataRow.Equals(dataTable.Rows[0]))
            //        {
            //            //  去掉第一行
            //            continue;
            //        }
            //        else
            //        {
            //            inventoryDtos.Add(
            //                new InventoryDto(
            //                    dataRow["Column1"].ToString().Replace("\r", "").Replace("\n", "")
            //                    , dataRow["Column2"].ToString().Replace("\r", "").Replace("\n", "")
            //                    , dataRow["Column3"].ToString().Replace("\r", "").Replace("\n", "")
            //                    , ""
            //                    )
            //                );
            //        }
            //    }
            //}
            //this.gridControl1.DataSource = inventoryDtos;
            //StockService.saveInventory(inventoryDtos);

        }

        private void 更改库存数量ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new Frm面料库存更改(
                FunctionHelper.GetgridViewValue(this.gridView1, this.gridView1.FocusedRowHandle, "materialCode")
                , FunctionHelper.GetgridViewValue(this.gridView1, this.gridView1.FocusedRowHandle, "materialSpec")
                , FunctionHelper.GetgridViewValue(this.gridView1, this.gridView1.FocusedRowHandle, "materialQuantity")).ShowDialog();
            this.list = FunctionHelper.JsonDeserialization<List<DTO库存一览>>(RestCall.httpGetMethod(URL.接口url + "/inventory/stockall"));
            this.gridControl1.DataSource = this.list;
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DataSet result = ExcelUtil.getExcelDatas();
            //
            DataTable testHD = result.Tables["TEST HD"];
            Dictionary<String, String> HD设计点 = new Dictionary<string, string>();
            for(int i = 9;i <= 23;i++)
            {
                HD设计点.Add(testHD.Rows[i]["Column0"].ToString(), testHD.Rows[i]["Column1"].ToString());
            }
            HD设计点.Add(testHD.Rows[25]["Column0"].ToString(), testHD.Rows[25]["Column1"].ToString());
            HD设计点.Add(testHD.Rows[26]["Column0"].ToString(), testHD.Rows[26]["Column1"].ToString());
            new DTOTest(testHD.Rows[7]["Column0"].ToString(), testHD.Rows[7]["Column1"].ToString(), HD设计点);

            //
            DataTable testHF = result.Tables["TEST HF"];
            Dictionary<String, String> HF设计点 = new Dictionary<string, string>();
            for (int i = 9; i <= 23; i++)
            {
                HF设计点.Add(testHF.Rows[i]["Column0"].ToString(), testHF.Rows[i]["Column1"].ToString());
            }
            HF设计点.Add(testHF.Rows[25]["Column0"].ToString(), testHF.Rows[25]["Column1"].ToString());
            HF设计点.Add(testHF.Rows[26]["Column0"].ToString(), testHF.Rows[26]["Column1"].ToString());
            new DTOTest(testHF.Rows[7]["Column0"].ToString(), testHF.Rows[7]["Column1"].ToString(), HF设计点);

            //
            DataTable testHH = result.Tables["TEST HH"];
            Dictionary<String, String> HH设计点 = new Dictionary<string, string>();
            for (int i = 9; i <= 23; i++)
            {
                HH设计点.Add(testHH.Rows[i]["Column0"].ToString(), testHH.Rows[i]["Column1"].ToString());
            }
            HH设计点.Add(testHH.Rows[25]["Column0"].ToString(), testHH.Rows[25]["Column1"].ToString());
            HH设计点.Add(testHH.Rows[26]["Column0"].ToString(), testHH.Rows[26]["Column1"].ToString());
            new DTOTest(testHH.Rows[7]["Column0"].ToString(), testHH.Rows[7]["Column1"].ToString(), HH设计点);

            //
            DataTable dataTableHDHF = result.Tables["Measurements HD HF"];
            Dictionary<String, String> HDHF测量 = new Dictionary<string, string>();
            for (int i = 14; i <= 23; i++)
            {
                HDHF测量.Add(dataTableHDHF.Rows[i]["Column0"].ToString(), dataTableHDHF.Rows[i]["Column5"].ToString());
            }
            new DTOMeasurements(dataTableHDHF.Rows[12]["Column1"].ToString(), HDHF测量);
            
            //
            DataTable dataTableHH = result.Tables["Measurements HH"];
            Dictionary<String, String> HH测量 = new Dictionary<string, string>();
            for (int i = 14; i <= 19; i++)
            {
                HH测量.Add(dataTableHH.Rows[i]["Column0"].ToString(), dataTableHH.Rows[i]["Column5"].ToString());
            }
            HH测量.Add(dataTableHH.Rows[24]["Column0"].ToString(), dataTableHH.Rows[24]["Column5"].ToString());
            HH测量.Add(dataTableHH.Rows[25]["Column0"].ToString(), dataTableHH.Rows[25]["Column5"].ToString());
            new DTOMeasurementsHH(dataTableHH.Rows[12]["Column1"].ToString(), HH测量);
        }
    }

    public class DTO尺寸
    {
        public String ItemCd { get; set; }

        public String ItemValue { get; set; }
        public String ItemSize { get; set; }
        public DTO尺寸(DataRow dataRow)
        {
            this.ItemCd = dataRow["Column0"].ToString();
            this.ItemValue = dataRow["Column1"].ToString();
            this.ItemSize = dataRow["Column3"].ToString();
        }
    }
    public class DTOTest
    {
        public String Item { get; set; }
        public String firstName { get; set; }
        public String lastName { get; set; }
        public String FabricNumber { get; set; }
        public Dictionary<String, String> map { get; set; } = new Dictionary<string, string>();
        public DTOTest(String Item,String FabricNumber,Dictionary<String,String> map)
        {
            this.Item = Item;
            this.FabricNumber = FabricNumber;
            this.map = map;
        }
        public DTOTest buildCustomer(String firstName,String lastName)
        {
            this.lastName = lastName;
            this.firstName = firstName;
            return this;
        }
    }
    public class DTO订单页
    {
        public String Item { get; set; }
        public String firstName { get; set; }
        public String lastName { get; set; }
        public String FabricNumber { get; set; }
        public String FIT_CD { get; set; }
        public Dictionary<String, String> map { get; set; } = new Dictionary<string, string>();
        public DTO订单页(String Item, String FabricNumber, Dictionary<String, String> map)
        {
            this.Item = Item;
            this.FabricNumber = FabricNumber;
            this.map = map;
        }
        public DTO订单页 buildCustomer(String firstName, String lastName)
        {
            this.lastName = lastName;
            this.firstName = firstName;
            return this;
        }
    }
    public class DTOMeasurements
    {
        public String Style { get; set; }//款号
        public String Size { get; set; }//尺寸
        public List<DTOUS坚山ExcelSize> dTOUS坚山ExcelSize { get; set; } = new List<DTOUS坚山ExcelSize>();

        //public Dictionary<String, String> map { get; set; } = new Dictionary<string, string>();
        public DTOMeasurements(String Size, Dictionary<String, String> map)
        {
            this.Size = Size;
            //this.map = map;
        }

        public DTOMeasurements(String Style,String Size,DataTable dt)
        {
            this.Style = Style;
            this.Size = Size;
            //for (int i = 16; i <= 27; i++)
            //{
            //    this.map.Add(dt.Rows[i]["Column0"].ToString().Trim(), dt.Rows[i]["Column5"].ToString().Trim());
            //}

            for(int i = 16; i <= 27; i++)
            {
                this.dTOUS坚山ExcelSize.Add(new DTOUS坚山ExcelSize(
                    dt.Rows[i]["Column0"].ToString().Trim(),
                    dt.Rows[i]["Column2"].ToString().Trim(),
                    dt.Rows[i]["Column3"].ToString().Trim(),
                    dt.Rows[i]["Column5"].ToString().Trim()
                    ));
            }
        }
    }
    public class DTOMeasurementsHH
    {
        public String Size { get; set; }

        public Dictionary<String, String> map { get; set; } = new Dictionary<string, string>();
        public DTOMeasurementsHH(String Size, Dictionary<String, String> map)
        {
            this.Size = Size;
            this.map = map;
        }
    }

    public class DTOUS坚山ExcelSize
    {
        public String sizeName { get; set; }//尺寸名称
        public String masterSize { get; set; }//试样衣尺寸
        public String adjustment { get; set; }//加减值
        public String garmentMeasurement { get; set; }//成衣尺寸

        public DTOUS坚山ExcelSize(string sizeName, string masterSize, string adjustment, string garmentMeasurement)
        {
            this.sizeName = sizeName;
            this.masterSize = masterSize;
            this.adjustment = adjustment;
            this.garmentMeasurement = garmentMeasurement;
        }
    }
}